﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UpdateServers.Command
{
   
    /// <summary>
    /// 日志记录
    /// </summary>
    public class Loger
    {
        private static readonly string methpath = AppDomain.CurrentDomain.BaseDirectory + "{0}\\";
        private static object files=new object();

        /// <summary>
        /// 日志记录（将文件放在根目录的ErrorLog文件夹内）
        /// </summary>
        /// <param name="value">内容</param>
        public static void Log(string value)
        {
            lock (files)
            {
                StreamWriter sw;
                var pathsite = string.Format(methpath, "Log");
                string filename = DateTime.Now.ToString("yyy-MM-dd") + ".txt";
                if (!Directory.Exists(pathsite))
                {
                    Directory.CreateDirectory(pathsite);
                }
                string path = pathsite + filename;
                if (!File.Exists(path))
                {
                    sw = File.CreateText(path);
                }
                else
                {
                    sw = File.AppendText(path);
                }
                string logtxt = string.Format("时间：{0}\r\n内容：{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), value);
                sw.WriteLine(logtxt);
                sw.Close();
                sw.Dispose();
            }
        }

        /// <summary>
        /// 日志记录
        /// </summary>
        /// <param name="value">日志内容</param>
        /// <param name="files">文件夹名</param>
        public static void Log(string value, string files)
        {
            lock (files)
            {
                StreamWriter sw;
                var pathsite = string.Format(methpath, files);
                string filename = DateTime.Now.ToString("yyy-MM-dd") + ".txt";
                if (!Directory.Exists(pathsite))
                {
                    Directory.CreateDirectory(pathsite);
                }
                string path = pathsite + filename;
                if (!File.Exists(path))
                {
                    sw = File.CreateText(path);
                }
                else
                {
                    sw = File.AppendText(path);
                }
                string logtxt = string.Format("时间：{0}\r\n内容：{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), value);
                sw.WriteLine(logtxt);
                sw.Close();
                sw.Dispose();
            }
        }

    }
}
